<template>
  <div class="write">
    <!--  -->
    <Editor @sendOrSave="PublishedArticles"></Editor>
  </div>
</template>

<script>
// 导入 editor 组件
import Editor from '@/components/Editor.vue'
import { mapState, mapActions } from 'vuex'
export default {
  name: 'Write',
  components: {
    Editor
  },
  data() {
    return {
      // 节流阀
      flag: false
    }
  },
  watch: {
  },
  computed: {
    ...mapState('cate', ['cate']),
    ...mapState('tag', ['tag'])
  },
  methods: {
    ...mapActions('write', ['addNewActicle']),
    saveDiscription() {},
    async PublishedArticles(childValue) {
      // console.log(this.formValue)
      console.log(childValue)
      if (childValue.title === '') { return this.$message.warning('文章标题不能为空') }
      if (childValue.imgUrl === '') { return this.$message.warning('文章封面图片地址不能为空') }
      if (childValue.describe === '') { return this.$message.warning('文章描述不能为空') }
      if (childValue.content === '') { return this.$message.warning('文章内容不能为空') }
      if (childValue.cateName === '') { return this.$message.warning('请选择文章分类') }
      if (childValue.tagName === '') { return this.$message.warning('请选择文章标签') }
      try {
        // 生成时间
        const date = new Date()
        // 年
        const y = date.getFullYear()
        // 月
        const m = date.getMonth() + 1
        // 日
        const d = date.getDate()
        // 时
        const h = date.getHours()
        // 分
        const mm = date.getMinutes()
        // 秒
        const s = date.getSeconds()
        const nowTime = `${y}-${m}-${d} ${h}:${mm}:${s}`
        // console.log(nowTime)
        // console.log(childValue)
        const params = {
          // 文章标题
          title: childValue.title,
          // 图片地址
          coverimg: childValue.coverimg,
          // 文章描述
          describes: childValue.describes,
          // 文章内容
          content: childValue.content,
          // 分类
          cateName: childValue.cateName,
          // 标签
          tagName: childValue.tagName,
          createTime: nowTime
        }
        await this.addNewActicle(params)
        this.$message.success('success')
        childValue = {
          // 文章标题
          title: '',
          // 图片地址
          coverimg: '',
          // 文章描述
          describes: '',
          // 文章内容
          content: '',
          // 分类
          cateName: '',
          // 标签
          tagName: ''
        }
      } catch (err) {
        this.$message.error(err.message)
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
